//
//  ComputingAngleModel.m
//  制作图标
//
//  Created by mac on 2017/8/31.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "ComputingAngleModel.h"

@implementation ComputingAngleModel
//计算两个点之间的距离

/**
 *  计算角度
 *  @param edge 线
 *  @return 角度
 */
//-(CGFloat)angleForStartPoint:(CGPoint)startPoint EndPoint:(CGPoint)endPoint{
//    
//    CGPoint Xpoint = CGPointMake(startPoint.x + 100, startPoint.y);
//    
//    
//    CGFloat a = endPoint.x - startPoint.x;
//    CGFloat b = endPoint.y - startPoint.y;
//    CGFloat c = Xpoint.x - startPoint.x;
//    CGFloat d = Xpoint.y - startPoint.y;
//    
//    CGFloat rads = acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
//    
//    if (startPoint.y>endPoint.y) {
//        rads = -rads;
//    }
//    return rads;
//}
+ (CGFloat)angleForOriginPoint:(CGPoint)originPoint StartPoint:(CGPoint)startPoint EndPoint:(CGPoint)endPoint{
    
    
    CGFloat a = distanceBetweenPoints(startPoint, endPoint);
    CGFloat b = distanceBetweenPoints(originPoint, startPoint);
    CGFloat c = distanceBetweenPoints(originPoint, endPoint);
    CGFloat X = (c*c + b*b - a*a) / (2*b*c);
        
    CGFloat rads = acos(X);
    if (DirectionOfRotationStraightLine == [self accordingToTheThreePointsWhetherClockwiseOrCounterclockwise_OnePoint:originPoint andTwopoint:startPoint andThreePoint:endPoint]) {
        //
    }else if (DirectionOfRotationClockwise == [self accordingToTheThreePointsWhetherClockwiseOrCounterclockwise_OnePoint:originPoint andTwopoint:startPoint andThreePoint:endPoint]){
    
        rads = -rads;
    }
//    if (startPoint.y<endPoint.y || startPoint.x > endPoint.x) {
//        rads = -rads;
//    }
   
    return rads;
}

//判断是顺时针还是逆时针
/*
 为正时，p1-p2-p3   路径的走向为逆时针，  
 为负时，p1-p2-p3   走向为顺时针，  
 为零时，p1-p2-p3   所走的方向不变，亦即三点在一直线上。
 */
+(NSInteger)accordingToTheThreePointsWhetherClockwiseOrCounterclockwise_OnePoint:(CGPoint)onepoint andTwopoint:(CGPoint)twopoint andThreePoint:(CGPoint)threePoint{
   
    
//    CGPoint p12 = CGPointMake(twopoint.x - onepoint.x, twopoint.y - onepoint.y);
//    CGPoint p23 = CGPointMake(twopoint.x - onepoint.x, twopoint.y - onepoint.y);
    float X12 = twopoint.x - onepoint.x;
    float X23 = threePoint.x - twopoint.x;
    float Y12 = twopoint.y - onepoint.y;
    float Y23 = threePoint.y - twopoint.y;
    
    float K = X12*Y23 - Y12*X23;
    if (K > 0) {
        return DirectionOfRotationCounterclockwise;
    }  
    if (K < 0){
        return DirectionOfRotationClockwise;
    } 
    
    return DirectionOfRotationStraightLine;
    
}

//两点之间的距离,算出缩放比
+(float)distanceFromPointX:(CGPoint)start distanceToPointY:(CGPoint)end andOriginPoint:(CGPoint)originPoint{
    float distance1;
    //下面就是高中的数学，不详细解释了
    CGFloat xDist = (end.x - originPoint.x); 
    CGFloat yDist = (end.y - originPoint.y); 
    distance1 = sqrt((xDist * xDist) + (yDist * yDist));
    
    float distance2;
    //下面就是高中的数学，不详细解释了
    CGFloat xDist2 = (start.x - originPoint.x); 
    CGFloat yDist2 = (start.y - originPoint.y); 
    distance2 = sqrt((xDist2 * xDist2) + (yDist2 * yDist2));
    return distance1 / distance2;    
}

CGFloat distanceBetweenPoints (CGPoint first, CGPoint second) {
    CGFloat deltaX = second.x - first.x;
    CGFloat deltaY = second.y - first.y;
    return sqrt(deltaX*deltaX + deltaY*deltaY );
};

bool CircleContainsPoint1(CGRect rect, CGPoint point)
{
    if (!CGRectContainsPoint(rect, point)) {
        return false;
    }
    
    CGFloat radius = rect.size.width<rect.size.height?rect.size.width/2:rect.size.height/2;
    CGPoint center = CGPointMake(rect.origin.x+rect.size.width/2, rect.origin.y+rect.size.height/2);
    CGPoint delta = CGPointMake(point.x-center.x, point.y-center.y);
    if (powf(delta.x, 2)+powf(delta.y, 2) < powf(radius, 2)) {
        return true;
    } else {
        return false;
    }
}



@end
